package com.sphd.restaurantsystem.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.Button;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.AddJiFenPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.AddJiFenView;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddJifenActivity extends BaseActivity implements AddJiFenView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.jifen_name_input)
    MaterialEditText jifenNameInput;
    @BindView(R.id.jifen_num_input)
    MaterialEditText jifenNumInput;
    @BindView(R.id.update_jifen)
    Button updateJifen;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    private String jifenname;
    private String jifennum;
    AddJiFenPresenterImpl addJiFenPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_jifen);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            finish();
        });
    }

    private void initView() {
        setTitleBar(toolbar, "积分商品编辑");
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        addJiFenPresenter = new AddJiFenPresenterImpl(this);
    }

    @OnClick(R.id.update_jifen)
    public void onClick() {
        jifenname = jifenNameInput.getText().toString().trim();
        jifennum = jifenNumInput.getText().toString().trim();
        if (!TextUtils.isEmpty(jifenname) && !TextUtils.isEmpty(jifennum)) {
            addJiFenPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.AddJifen + "cgid=" + restaurantInfo.getId()
                    + "&integral=" + jifennum + "&dishes=" + StringUtils.utf8Encode(jifenname));
        } else {
            toast("积分编辑不能为空");
        }
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("添加成功");
            setResult(0);
            finish();
        } else {
            toast("添加失败，请重试！");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
